package com.william.jdbcplus.core.util.druid;

import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVisitor;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

public class OracleOutputVisitor extends com.alibaba.druid.sql.dialect.oracle.visitor.OracleOutputVisitor implements OracleASTVisitor {

    public OracleOutputVisitor(Appendable appender) {
        super(appender);
    }

    public OracleOutputVisitor(Appendable appender, boolean printPostSemi) {
        super(appender, printPostSemi);
    }

    @Override
    public void print(Date date) {
        if (this.appender == null) {
            return;
        }

        String DateSql = formatDate(date);
        print0(DateSql);
    }

    private String formatDate(Date date) {
        if (date instanceof Timestamp) {
            return "to_timestamp('" + new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS").format(date) + "', 'mm/dd/yyyy hh24:mi:ss.ff3')";
        } else if (date instanceof Date) {
            return "to_date('" + new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(date) + "', 'mm/dd/yyyy hh24:mi:ss')";
        } else {
            return "to_date('" + new SimpleDateFormat("yyyy-MM-dd").format(date) + "', 'yyyy-MM-dd')";
        }
    }

}
